package test.action;

import java.util.List;
import java.util.logging.Logger;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import test.model.User;
import test.service.UserService;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;


@Component("userAction")
@Scope("prototype")
public class UserAction extends ActionSupport implements ModelDriven<User>, Preparable {

	/**
	 * 
	 */
	private static final long serialVersionUID = -5511353482856958122L;
	private UserService userService;
	private User user;
	private List<User> userList;

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public List<User> getUserList() {
		return userList;
	}

	public void setUserList(List<User> userList) {
		this.userList = userList;
	}

	public UserService getUserService() {
		return userService;
	}

	@Resource
	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public String addUser() {
		Logger.getAnonymousLogger().info("add action");
		if (userService.exits(user.getUsername())) {
			return ERROR;
		}
		userService.save(user);
		setSession(user);
		return SUCCESS;
	}

	public String listUser() {
		Logger.getAnonymousLogger().info("list action");
		this.userList = userService.list();
		boolean resul = chargeSession();
		return "list";
	}
	
	private void setSession(User user) {
	     
	     if (null != user) {
	    	 HttpSession session = ServletActionContext.getRequest().getSession(true); 
	    	 

	         if (null != session) {
	        	 System.out.println("getSession() seccess!!! + id = "+ session.getId());
	             session.setAttribute( "USER_SESSION", user );
	         } else
	        	 System.out.println("getSession() :null = session");
	     } else
	    	 System.out.println("setSession: null = user");
	}
	
	private boolean chargeSession() {
	     
	
	    	 HttpSession session = ServletActionContext.getRequest().getSession(false); 
	    	 User user = null;

	         if (null != session) {
	        	 System.out.println("chargeSession getSession() seccess!!! + id = "+ session.getId());
	        	 user =(User) session.getAttribute( "USER_SESSION");
	        	 if (user != null)
	        	 {
	        		 System.out.println("user.getUsername() ="+ user.getUsername() );
	        		 System.out.println("user.getPassword() ="+ user.getPassword() );
	        		 return true;
	        	 }else {
	        		 System.out.println("session has not user!!!" );
	        		 return false;
				}
	        	 
	             
	         } else
	         {
	        	 System.out.println(" chargeSession getSession() :null = session");
	        	 return false;
	         }
	        	 
	             

	}

	public User getModel() {
		// TODO 自动生成方法存根
		return user;
	}

	public void prepare() throws Exception {
		// TODO 自动生成方法存根
		
	}	
	
}
